package com.coolnie.controller;

import com.coolnie.entity.User;
import com.coolnie.entity.UserException;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/29 0029.
 */
@Controller
//路径
@RequestMapping("/user")
public class UserController {
//    private static final TestLogger LOGGER = Logger.getLogger(TestUserService.class);


//    private static final Logger LOGGER = Logger.getLogger(TestUserService.class);

    private Map<String ,User>users=new HashMap<String, User>();

    public UserController(){
        users.put("coolwen",new User("coolwen","wen","123"));
        users.put("coolchen",new User("coolchen","chen","123"));
        users.put("coolgon",new User("coolgon","gon","123"));
    }
/**
 *列举出所有用户的信息
 */

    @RequestMapping(value="/users",method = RequestMethod.GET)
    public String listUser(Model model){
        model.addAttribute("users",users);//传递数据到前台界面
        return "user/list";
    }

    /*跳转*/
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public String addUser(Model model){
        model.addAttribute(new User());
        return "user/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addUser(@Validated User user, BindingResult br, MultipartFile[] attachs, HttpServletRequest req) throws IOException {
        if (br.hasErrors())
            return "user/add";//服务器
        for (MultipartFile attach:attachs) {
//            System.out.println(attach.getOriginalFilename() + ";" + attach.getContentType() + ";" + attach.getName());
            if (attach.isEmpty())
                continue;
            String realPath = req.getSession().getServletContext().getRealPath("/resouces/upload");
            System.out.println(realPath);
            File file = new File(realPath + "/" + attach.getOriginalFilename());
            FileUtils.copyInputStreamToFile(attach.getInputStream(), file);
        }
        System.out.println(user.toString());
        users.put(user.getUsername(),user);
        return "redirect:/user/users";//客户端
    }
    /*/user/username*/
@RequestMapping(value = "/{username}",method = RequestMethod.GET)
public  String showUser(Model model,@PathVariable String username){
    System.out.println(username);
    System.out.println(users.get(username));
    model.addAttribute("user",users.values().equals(username));
    return "user/user";
}
@RequestMapping(value = "/{username}/delete",method = RequestMethod.GET)
public String delUser(@PathVariable String username){
    System.out.println(username);
    users.remove(username);
    return "redirect:/user/users";
    }

    @RequestMapping(value = "/{username}/update",method = RequestMethod.GET)
    public String updateUser(@PathVariable String username,Model model){
        model.addAttribute("user",users.get(username));
        return "user/update";
    }
    @RequestMapping(value = "/{username}/update",method = RequestMethod.POST)
    public String updateUser(@Validated User user, BindingResult br, MultipartFile attach){
        if(br.hasErrors())
            return "user/update";


            users.put(user.getUsername(),user);
            return "redirect:/user/users";
        }
@RequestMapping(value = "/login",method = RequestMethod.POST)
        public String login(String username,String password,HttpSession session){
    System.out.println("UserName:"+username+"PassWord:"+password);
    if(!users.containsKey(username))
        throw new UserException("用户名不存在");
    if (users.get(username).getPassword().equals(password))
        throw new UserException("密码不正确");
    session.setAttribute("LoginUser",users.get(username));
            return "redirect:/user/users";
        }
//    @RequestMapping(value = "/{username}",method = RequestMethod.GET,params = "json")
//    @ResponseBody
//    public  User showUser(@PathVariable String username){
//        LOGGER.info(users.get(username));
//        return users.get(username);
//    }
    
}
